package unitTests;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import increment1.*;

import static org.junit.Assert.*;

import org.junit.Test;

/**
 * TaskViewUnitTests class contains the units tests of the methods in the TaskView class 
 * 
 * @author Amer Alhalabi
 *
 */
public class TaskViewUnitTests {

	public TaskViewUnitTests() {
    }
	
	private void createFile( String fileName, String content) {
		PrintWriter printOut = null; 
		try {
			printOut = new PrintWriter(fileName); 
			printOut.print(content); 
			printOut.close();
		}
		catch (FileNotFoundException e) {
			System.out.println(e.getMessage());
		}
	}
	
	private void deleteFile(String fileName) {
		File file = new File(fileName);
		file.delete();
	}
	
	@Test
	public void testTaskViewGetInstance1() {
       
		TaskView obj = TaskView.getInstance();
		assertNotNull(obj);
		assertTrue(obj.getTaskList().isEmpty());
    }
	
	@Test
	public void testTaskViewGetInstance2() {
       
		TaskView obj1 = TaskView.getInstance();
		TaskView obj2 = TaskView.getInstance();
		TaskView obj3 = TaskView.getInstance();
		
		assertNotNull(obj1);
		assertSame(obj1, obj2);
		assertSame(obj2, obj3);
    }
	
	@Test
	public void testTaskViewOpenFile1() {
		// create an empty file
		String filename = "test.txt";
		String content = "";
		
		createFile(filename, content);
		
		TaskView obj = TaskView.getInstance(); 
		assertTrue(obj.openFile(filename));
		assertTrue(obj.getTaskList().isEmpty());
		deleteFile(filename);
	}
	
	@Test
	public void testTaskViewOpenFile2() {
		String filename = "doesNotExist.txt";
		TaskView obj = TaskView.getInstance(); 
		assertFalse(obj.openFile(filename));
		assertTrue(obj.getTaskList().isEmpty());
	}
	
	@Test
	public void testTaskViewOpenFile3() {
		// create a file
		String filename = "test.txt";
		String content = "Task 1\n";
		content += "Iteration 1\n";
		content += "The first iteration of the project, including requirements document and source code\n";
		content += "2\n";
		content += "Requirements document\n";
		content += "2012-01-27 23:59:59\n";
		content += "Person 1";
		
		createFile(filename, content);
		
		TaskView obj = TaskView.getInstance(); 
		
		assertTrue(obj.openFile(filename));
		assertEquals(obj.getTaskList().size(), 1);
		assertEquals(PeopleView.getInstance().getPersonList().size(), 1);
		
		deleteFile(filename);
	}
	
	@Test
	public void testTaskViewOpenFile4() {
		// create a file
		String filename = "test.txt";
		String content = "Task 1\n";
		content += "Iteration 1\n";
		content += "The first iteration of the project, including requirements document and source code\n";
		content += "2\n";
		content += "Requirements document\n";
		content += "2012-01-27 23:59:59\n";
		content += "Person 1\n";
		content += "Task 2\n"; 
		content += "Iteration 2\n";
		content += "The second iteration of the project, including design document and source code\n"; 
		content += "4\n"; 
		content += "Design document\n"; 
		content += "2012-02-24 23:59:59\n"; 
		content += "Person 1\n";
		content += "Task 3\n"; 
		content += "Iteration 3\n"; 
		content += "The third iteration of the project, including test plan, report document and source code\n"; 
		content += "30\n"; 
		content += "Test plan and report document\n"; 
		content += "2012-03-16 23:59:59\n"; 
		content += "Person 2\n";
		
		createFile(filename, content);
		
		TaskView obj = TaskView.getInstance(); 
		
		assertTrue(obj.openFile(filename));
		assertEquals(obj.getTaskList().size(), 3);
		assertEquals(PeopleView.getInstance().getPersonList().size(), 2);
		
		deleteFile(filename);
	}
	
	@Test
	public void testTaskViewOpenFile5() {
		// create a file
		String filename = "test.txt";
		String content = "Task 1\n";
		content += "Iteration 1\n";
		content += "The first iteration of the project, including requirements document and source code\n";
		content += "2\n";
		content += "Requirements document\n";
		content += "2012-01-27 23:59:59\n";
		content += "Person 1\n";
		content += "Task 2\n"; 
		content += "Iteration 2\n";
		content += "The second iteration of the project, including design document and source code\n"; 
		content += "4\n"; 
		content += "Design document\n"; 
		content += "2012-02-24 23:59:59\n"; 
		content += "Person 1\n";
		content += "Task 1\n"; 
		content += "Iteration 3\n"; 
		content += "The third iteration of the project, including test plan, report document and source code\n"; 
		content += "30\n"; 
		content += "Test plan and report document\n"; 
		content += "2012-03-16 23:59:59\n"; 
		content += "Person 2";
		
		createFile(filename, content);
		
		TaskView obj = TaskView.getInstance(); 
		
		assertFalse(obj.openFile(filename));
		assertEquals(obj.getTaskList().size(), 0);
		assertEquals(PeopleView.getInstance().getPersonList().size(), 0);
		
		deleteFile(filename);
	}
	
	@Test
	public void testTaskViewOpenFile6() {
		// create a file
		String filename = "test.txt";
		String content = "Task 1\n";
		content += "Iteration 1\n";
		content += "The first iteration of the project, including requirements document and source code\n";
		content += "2\n";
		content += "Requirements document\n";
		content += "2012-01-27 23:59:59\n";
		content += "Person 1\n";
		content += "Task 2\n"; 
		content += "Iteration 2\n";
		content += "The second iteration of the project, including design document and source code\n"; 
		content += "4\n"; 
		//content += "Design document\n"; missing attribute 
		content += "2012-02-24 23:59:59\n"; 
		content += "Person 1\n";
		content += "Task 3\n"; 
		content += "Iteration 3\n"; 
		content += "The third iteration of the project, including test plan, report document and source code\n"; 
		content += "30\n"; 
		content += "Test plan and report document\n"; 
		content += "2012-03-16 23:59:59\n"; 
		content += "Person 2";
		
		createFile(filename, content);
		
		TaskView obj = TaskView.getInstance(); 
		
		assertFalse(obj.openFile(filename));
		assertEquals(obj.getTaskList().size(), 0);
		assertEquals(PeopleView.getInstance().getPersonList().size(), 0);
		
		deleteFile(filename);
	}
	
	@Test
	public void testTaskViewOpenFile7() {	
		String filename = "";

		TaskView obj = TaskView.getInstance(); 
		
		assertFalse(obj.openFile(filename));
		assertEquals(obj.getTaskList().size(), 0);
		assertEquals(PeopleView.getInstance().getPersonList().size(), 0);
		
	}
	
	@Test
	public void testTaskViewOpenFile8() {
	
		String filename = null;
		TaskView obj = TaskView.getInstance(); 
		
		assertFalse(obj.openFile(filename));
		assertEquals(obj.getTaskList().size(), 0);
		assertEquals(PeopleView.getInstance().getPersonList().size(), 0);
	}
}
